package com.example.mercury.login;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatButton;

import com.example.mercury.R;
import com.example.mercury.entity.User;
import com.example.mercury.utils.HttpUtils;
import com.google.gson.Gson;

import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * @author 盛守义
 * @createTime 2021/10/24 15:43
 * @projectName TurningCoffee
 * @className ForgetPwdActivity.java
 * @description TODO
 */
public class ForgetPwdActivity extends Activity {

    private EditText et_mail, et_verifyfgtpwd;
    private AppCompatButton btn_verifyfgtpwd, btn_submitfgtpwd;
    private String email, confirmCode;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forgetpwd);
        initViews();
        setListener();
    }

    class ForgetPwdOnClickListener implements View.OnClickListener{

        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.btn_verifyfgtpwd:
                    email = et_mail.getText().toString().trim();
                    if(email == null|| email.equals("")){
                        Toast.makeText(getApplicationContext(), "邮箱不能为空", Toast.LENGTH_SHORT).show();
                    }else{
                        Gson gson = new Gson();

                        User user = new User();
                        user.setUserTel(email);

                        String jsonString = gson.toJson(user);

                        HttpUtils httpUtils = new HttpUtils();
                        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), jsonString);

                        //url
                        Call call = httpUtils.getCall(requestBody, "");
                    }
                    break;
                case R.id.btn_submitfgtpwd:
                    email = et_mail.getText().toString().trim();
                    confirmCode = et_verifyfgtpwd.getText().toString().trim();
                    if(confirmCode == null|| confirmCode.equals("")){
                        Toast.makeText(getApplicationContext(), "请输入验证码", Toast.LENGTH_SHORT).show();
                    }
                    //提交验证码和邮箱
                    //返回json
                    //将map中success判断


                    Intent intent = new Intent(ForgetPwdActivity.this, ResetPasswordActivity.class);
                    intent.putExtra("usermail", email);
                    break;
            }
        }
    }


    void initViews(){
        et_mail = findViewById(R.id.et_mailfgtpwd);
        et_verifyfgtpwd = findViewById(R.id.et_verifyfgtpwd);
        btn_submitfgtpwd = findViewById(R.id.btn_submitfgtpwd);
        btn_verifyfgtpwd = findViewById(R.id.btn_verifyfgtpwd);
    }

    void setListener(){
        ForgetPwdOnClickListener listener = new ForgetPwdOnClickListener();
        btn_submitfgtpwd.setOnClickListener(listener);
        btn_verifyfgtpwd.setOnClickListener(listener);
    }
}
